﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using Service;


namespace ItaServicos.Controllers
{
    public class ClienteController : Controller
    {
        private GerenciadorCliente gCliente;
        

        public ClienteController()
        {
            gCliente = new GerenciadorCliente();
            
        }


        //
        // GET: /Cliente/

        public ActionResult Index()
        {

            ViewBag.Message = "ItaServicos";
            return View(gCliente.ObterTodos());
        }

        
        //
        // GET: /Cliente/Create
        [HttpGet]
        public ActionResult Create()
        {


            return View();
        }

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(ClienteModel cliente)
        {

            try
            {
                if (!ModelState.IsValid)
                {
                    gCliente.Inserir(cliente);
                    return RedirectToAction("Index");

                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            ClienteModel cliente = gCliente.Obter(id);

            
            return View(cliente);
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(ClienteModel cliente)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    gCliente.Editar(cliente);
                    return RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Delete/5

        [HttpGet]
        public ActionResult Delete(int id)
        {

            ClienteModel cliente = gCliente.Obter(id);
            return View(cliente);
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                gCliente.Remover(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
